package core.toolbox.ModelUtils;

import com.jfinal.plugin.activerecord.Record;

import java.util.*;

/**
 * Created by Administrator on 2017/3/16.
 */
public class ModelUtils {

    public static Record getRecordInsideList(Map<String, String[]> data){
        Record result = new Record();
        Set<Map.Entry<String, String[]>> entries = data.entrySet();
        Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
        while(iterator.hasNext()){
            Map.Entry<String, String[]> next = iterator.next();
            String key = next.getKey();
            String[] vals = next.getValue();
            if(vals.length == 1){
                result.set(key, vals[0]);
            }else{
                result.set(key, Arrays.asList(vals));
            }
        }
        return result;
    }

    /**
     * 该功能已被BaseController.getParaData替代
     * @param data
     * @param listSize
     * @return
     */
    public static Record getRecordInsideListMap(Map<String, String[]> data, int listSize){
        Record recordInsideList = getRecordInsideList(data);
        Map<String, Object> columns = recordInsideList.getColumns();
        Map<String,List> list = new LinkedHashMap();
        Set<Map.Entry<String, Object>> entries = columns.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while(iterator.hasNext()){
            Object value = iterator.next().getValue();
            if(value instanceof List && ((List) value).size()==listSize){
                list.put(iterator.next().getKey(),(List) value);
                recordInsideList.remove(iterator.next().getKey());
            }
        }
        List<Record> insidedata = new LinkedList();
        for(int i = 0; i < listSize; i++){
            Record r = new Record();
            Set<Map.Entry<String, List>> entries1 = list.entrySet();
            Iterator<Map.Entry<String, List>> iterator1 = entries1.iterator();
            while(iterator1.hasNext()){
                Map.Entry<String, List> next = iterator1.next();
                r.set(next.getKey(), next.getValue().get(i));
            }
        }
        recordInsideList.set("_data", insidedata);

        return recordInsideList;
    }



}
